import { JSEncrypt } from "jsencrypt";
// AES加密算法(这个项目用不到了)
const publicKey = "";

export function encrypt(str) {
	let encryptor = new JSEncrypt();
	encryptor.setPublicKey(publicKey);
	return encryptor.encrypt(str);
}

// 获取路由变量
export function getUrlParams(url) {
	if (!/\?/.test(url)) {
		return {};
	}
	let str = `?${url.split("?")[1]}`;
	return Object.fromEntries(new URLSearchParams(str));
}

// 下载二进制文件的方法
export function downLoadBlobFile(data, config) {
	if (!data) {
		throw new Error("downLoadBlobFile方法：没有数据传递");
	}
	if (!config || JSON.stringify(config) === "{}") {
		throw new Error(
			"downLoadBlobFile方法：没有传递第二个参数,格式应该为 {applicationType:xxx,fileName:xxx}"
		);
	}
	const { applicationType = "", fileName = "" } = config;
	if (!applicationType || !fileName) {
		throw new Error(
			"downLoadBlobFile方法：config中没有传递正确的参数,{applicationType:xxx,fileName:xxx}"
		);
	}
	let blob = new Blob([data], { type: applicationType });
	let url = window.URL.createObjectURL(blob);
	let dom = document.createElement("a");
	dom.setAttribute("download", fileName);
	dom.setAttribute("href", url);
	dom.click();
	window.URL.revokeObjectURL(url);
	dom = null;
	url = null;
	blob = null;
}

// 返回长链路对象
export function getObjectFinalValue(obj, str) {
	if (!obj || JSON.stringify(obj) === "{}") {
		return undefined;
	}
	if (!str) {
		throw new Error("getObjectFinalValue方法：没有传递第二个参数");
	}
	let arr = str.split(".");
	let newObj = obj;
	for (let i of arr) {
		newObj = newObj[i];
		if (!newObj) {
			return undefined;
		}
	}
	return newObj;
}

// 获取树图
export function getTree(data = [], config) {
	if (!config || JSON.stringify(config) === "{}") {
		throw new Error(
			"getTree方法：缺少第二个参数,格式为 {selfId:'xxx',parentId:'xxx'}"
		);
	}
	const { selfId = "deptId", parentId = "parentId" } = config;
	if (!data || !data.length) {
		return [];
	}
	// eslint-disable-next-line
	let options = data.filter((item) => {
		let originArr = data.filter(
			(sonItem) => sonItem[selfId] === item[parentId]
		);
		if (!originArr.length) {
			return item;
		}
	});
	function addChildren(theData) {
		let optionsC = theData.map((item) => {
			let children = data.filter((it) => it[parentId] === item[selfId]);
			if (!!children.length) {
				item.children = addChildren(children);
			}
			return item;
		});
		return optionsC;
	}
	options = addChildren(options);
	return options;
}

// 获取教材树状图
export function getTreeB(data) {
	let arr = [];
	let obj = null;
	data.forEach((item) => {
		obj = JSON.stringify({
			label: item.bookCategoryFirst,
			value: item.bookCategoryFirstId,
		});
		if (!arr.includes(obj)) {
			arr.push(obj);
		}
	});
	arr = arr.map((item) => JSON.parse(item));
	arr.forEach((itemA) => {
		let arrB = data
			.filter((itemB) => itemB.bookCategoryFirstId === itemA.value)
			.map((itemC) => {
				return JSON.stringify({
					label: itemC.bookCategorySecond,
					value: itemC.bookCategorySecondId,
				});
			});
		if (!!arrB.length) {
			itemA.children = [...new Set(arrB)].map((item) => {
				return JSON.parse(item);
			});
			itemA.children.forEach((itemB) => {
				let arrC = data
					.filter(
						(itemC) => itemC.bookCategorySecondId === itemB.value
					)
					.map((itemD) => {
						return {
							label: itemD.bookCategoryThird,
							value: itemD.bookCategoryThirdId,
						};
					});
				if (!!arrC.length) {
					itemB.children = arrC;
				}
			});
		}
	});
	return arr;
}

// 获取树状链路数组值
export function getCascaderArr(data, tree, field) {
	let arr = [];
	arr = tree.filter((item) => data === item[field]);
	if (!!arr.length) {
		return [arr[0][field]];
	} else {
		let newArr = tree.filter(
			(item) => !!item.children && !!item.children.length
		);
		if (!newArr.length) {
			return [];
		}
		for (let item of newArr) {
			arr = [item[field], ...getCascaderArr(data, item.children, field)];
			if (arr.length > 1) {
				return arr;
			}
			continue;
		}
		return [];
	}
}
